/**
 * @(#)UserResource.java 2011
 *
 *
 * Copyright 2011 classic-commerce
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package ca.ioniq.server.resource.impl;

import javax.inject.Inject;
import javax.ws.rs.PathParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import ca.ioniq.server.resource.UserResource;
import ca.ioniq.server.service.AuthenticationService;
import ca.ioniq.server.service.UserService;
import ca.ioniq.server.service.exception.AuthenticationException;
import ca.ioniq.server.service.exception.UserException;
import ca.ioniq.shared.entity.User;

/**
 * @author: <a href="mailto:richard@ioniq.ca">Richard Mutezintare</a>
 */

public class UserResourceImpl implements UserResource {

    private UserService userService;
    private AuthenticationService authenticationService;

    @Inject
    public UserResourceImpl(UserService userService, AuthenticationService authenticationService) {
        this.userService = userService;
        this.authenticationService = authenticationService;
    }

    public User get(@PathParam("id") Long id) {
    	return userService.findUser(id);
    }

    public User put(User user) {
        return userService.register(user);
    }

    public User update(User user) {
			try {
				return userService.update(user);
			} catch (UserException e) {
				throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
			}
    }

    public void delete(@PathParam("id") Long id) {
		try {
			userService.deleteUser(id);
		} catch (UserException e) {
			throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
		}
    }
    
	public String authenticate(User user) {
		try {
			return new Boolean(authenticationService.authenticate(user)).toString();
		} catch (AuthenticationException e) {
			throw new WebApplicationException(e, Response.Status.BAD_REQUEST);
		}
	}

}
